<?php
declare (strict_types=1);

namespace app\user\model\teacher\admin;

use app\common\logic\Attach;
use app\common\model\BaseModel;
use app\school\model\school\School;
use app\user\event\subscribes\user\admin\Teacher as TeacherSubscribe;
use app\user\model\user\admin\Account;
use think\facade\Event;
use think\model\concern\SoftDelete;

/**
 * 定义讲师模型
 */
class Teacher extends BaseModel
{
    use SoftDelete;

    /**
     * 表名
     *
     * @var string
     */
    protected $name = 'teacher';

    /**
     * 设置自动写入时间
     *
     * @var boolean
     */
    protected $autoWriteTimestamp = true;

    /**
     * 设置字段
     *
     * @var array
     */
    protected $append = ['from_text', 'status_text'];

    /**
     * 定义字段类型
     *
     * @var array
     */
    protected $type = [
        'id'               => 'integer',
        'status'           => 'integer',
        'fullcategorypath' => 'json',
    ];

    /**
     * 定义表主键
     *
     * @var string
     */
    protected $pk = 'id';

    // 定义全局的查询范围
    protected $globalScope = ['mhm'];

    /**
     * 限制机构条件
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-06-16
     *
     * @param    [type]                         $query [description]
     * @return   [type]                                [description]
     */
    public function scopeMhm($query)
    {
        // 如果不是平台时, 加入机构条件限制
        if (!request()->is_platform) {
            $query->where('mhm_id', request()->mhm_id);
        }
    }

    public function scopeWaiting($query)
    {
        $query->where('status', 2)->where('mhm_id', request()->mhm_id);
    }

    /**
     * 新增数据事件
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-03-03
     *
     * @return   string
     */
    public static function onBeforeInsert($teacher)
    {
        $teacher->from   = 'admin';
        $teacher->mhm_id = $teacher->getAttr('mhm_id') ?: request()->mhm_id;
        $teacher->status = 1;
    }

    /**
     * 成功创建之后
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-06-18
     */
    public static function onAfterInsert($teacher)
    {
        // 清理推荐课程缓存
        cache('recommend_teacher', null);

        if (!Event::hasListener('TeacherCreated')) {
            Event::subscribe(TeacherSubscribe::class);
        }
        Event::trigger('TeacherCreated', $teacher);
    }

    /**
     * 成功修改之后
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-06-18
     */
    public static function onAfterUpdate($teacher)
    {
        // 清理推荐课程缓存
        cache('recommend_teacher', null);

        if (!Event::hasListener('TeacherUpdated')) {
            Event::subscribe(TeacherSubscribe::class);
        }
        Event::trigger('TeacherUpdated', $teacher);
    }

    /**
     * 成功删除之后
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-06-18
     */
    public static function onAfterDelete($teacher)
    {
        // 清理推荐课程缓存
        cache('recommend_teacher', null);

        if (!Event::hasListener('TeacherDeleted')) {
            Event::subscribe(TeacherSubscribe::class);
        }
        Event::trigger('TeacherDeleted', $teacher);
    }

    /**
     * 机构来源
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-05-27
     *
     * @param string $value [description]
     * @param    [type]                         $data  [description]
     * @return   [type]                                [description]
     */
    public function getFromTextAttr($value = '', $data): string
    {
        $text = [
            'admin' => '管理员创建',
        ];

        return $text[$data['from']] ?? $data['from'] . '端申请认证';
    }

    /**
     * 讲师状态
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-06-04
     *
     * @param string $value [description]
     * @param    [type]                         $data  [description]
     * @return   [type]                                [description]
     */
    public function getStatusTextAttr($value = '', $data): string
    {
        $text = [
            0 => '讲师被禁用',
            1 => '正常',
            2 => '待审核',
            3 => '审核未通过',
        ];

        return $text[$data['status']] ?? '未知';
    }

    /**
     * 设置状态查询条件
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-03-10
     *
     * @param    [type]                         $query  [description]
     * @param integer $status 查询条件值
     */
    public function scopeStatus($query, $status = 1)
    {
        $query->where('status', $status);
    }

    /**
     * 关联的用户
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-04-16
     *
     * @return   [type]                         [description]
     */
    public function user()
    {
        return $this->belongsTo(Account::class, 'user_id')
            ->removeOption('soft_delete')
            ->removeWhereField('mhm_id')
            ->with(['avatar_url'])
            ->bind([
                'teacher_phone' => 'phone',
                'teacher_name'  => 'nick_name',
                'signature',
                'avatar_url'
            ]);
    }

    /**
     * 关联的机构
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-04-16
     *
     * @return   [type]                         [description]
     */
    public function school()
    {
        return $this->belongsTo(School::class, 'mhm_id')->bind(['school_name' => 'title']);
    }

    /**
     * 机构等级
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-05-28
     *
     * @return   [type]                         [description]
     */
    public function level()
    {
        return $this->belongsTo(TeacherLevel::class, 'level_id')->bind(['level_text' => 'title']);
    }

    /**
     * 身份证-正面
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-05-28
     *
     * @return   [type]                         [description]
     */
    public function iDcardPositive()
    {
        return $this->belongsTo(Attach::class, 'IDcard_positive')->bind(['IDcard_positive_url' => 'fileurl']);
    }

    /**
     * 身份证-反面
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-05-28
     *
     * @return   [type]                         [description]
     */
    public function iDcardSide()
    {
        return $this->belongsTo(Attach::class, 'IDcard_side')->bind(['IDcard_side_url' => 'fileurl']);
    }

    /**
     * 执照
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-05-28
     *
     * @return   [type]                         [description]
     */
    public function certification()
    {
        return $this->belongsTo(Attach::class, 'certification')->bind(['certification_url' => 'fileurl']);
    }
}
